/**
* @Title: insuredBlacklist_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:46:06
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('AddMarkScoreDialogCtr', ['$scope', '$http','BaseURL', 'Modal','$modalInstance','BaseService','CommunityService', function($scope, $http, baseUrl, Modal,$modalInstance,BaseService
			,CommunityService){
		
		
	
		/**
		 * 扣分设置保存按钮
		 */
		$scope.confirm=function(){
			
			var markRuleId=$scope.markRuleId;
			
			if($scope.insuranceRows.length>0){
				
				var flag=true;
				
				//验证格式
				$.each($scope.insuranceRows,function(index,item){
					var isValid=item.isValid;
					var scoreName=item.scoreName;
					var upperValues=item.upperValues;
					var test=/^[0-9]+(\.[0-9]+){0,1}$/;
					
				
					 if(isValid==='0'){ //新增的
						  if(!scoreName){
							  flag=false;
							  Modal.alert("请输入扣分值描述!");
							  return false;
						  }
						  if(!test.test(upperValues)){
							  flag=false;
							  Modal.alert("请输入正确的扣分值");
							  return false;
						  }
					 }
					
				});
				
				
				var parameter={
						markRuleId:markRuleId, //医院考评规则
						list:$scope.insuranceRows
				             };
				
				 if(flag){
					 
					 $http({
							method : 'post',
							cache : false,
							url : baseUrl + 'evaluationRule/saveMarkRuleValue.do',
							params :parameter
						}).success(function(response, status, headers, config) {
							 if(response){
								   if(response.flag){
									    if($scope.categoryType=='0'){ //医院
									    	$scope.flashOrgCurrentPage();
									    	$scope.onClose();
									    }else if($scope.categoryType=='2'){ //药店
									    	$scope.flashStoryCurrentPage();
									    	$scope.onClose();
									    }
								   }
							 }
							
						});
					 
					 
					 
				 }
			
			}else{
				$scope.onClose();
			}
			
		}
		

		
		//考核内容显示
    	$scope.insuranceRows = [];
    	$.each($scope.scoreList,function(index,item){
    		var name=item.markValueName;
    		var score=item.markValueScore;
    		var obj={
    				scoreName:name,
    			    upperValues:score,
    			    isValid: "0",
    			    flag:"1"
    		};
    		$scope.insuranceRows.push(obj);
    	
    	});
    	
    	
    	//新增考核内容
    	$scope.insuranceAdd = function(){
    			var row = {"scoreName": "","upperValues": "","isValid": "0","flag":"0"};
	    		 $scope.insuranceRows.push(row);
    	}
    	
    	
    	//选中的考核内容
    	$scope.insuranceSelectRow = null;
    	
    	//选中一行考核内容
    	$scope.checkedInsuranceRow = function(data) {
    		$scope.insuranceSelectRow = data;
    		
    		angular.forEach($scope.insuranceRows,function(insuranceData,index){
    			if (data === insuranceData) {
    				insuranceData.checked = true;
    			}
    			else {
    				insuranceData.checked = false;
    			}
    		});
    	}
    	
    	
    	//删除考核内容
    	$scope.insuranceDel = function(){
    		if ($scope.insuranceSelectRow) {
    			
    			Modal.confirm("确定删除选中的扣分值吗?",function(){
    				
    				angular.forEach($scope.insuranceRows,function(insuranceData,index){
    	    			if ($scope.insuranceSelectRow === insuranceData) {
    	    				insuranceData.isValid = 1;
    	    			}
    	    		});
    				
    				
	    		});
    		} 
    		else {
    			Modal.alert("请选中一项扣分值进行操作!!!");
    		}
    		
    	}
		
		//关闭model
		$scope.onClose = function() {
			$modalInstance.close();
		}

		
	}]);
})();